import os


GCC_ARM_PATH = '/usr/local/arm/gcc-arm-9.2-2019.12-x86_64-arm-none-eabi'
PREFIX = GCC_ARM_PATH + '/bin/arm-none-eabi-'

CC = PREFIX + 'gcc'
AS = PREFIX + 'as'
AR = PREFIX + 'ar'
CXX = PREFIX + 'g++'
LINK = PREFIX + 'ld'

SIZE = PREFIX + 'size'
OBJDUMP = PREFIX + 'objdump'
OBJCPY = PREFIX + 'objcopy'


TARGET_PATH = './'
TARGET_NAME = 'SoC2018_RTT'
TARGET_NO_SUFFIX = TARGET_PATH + TARGET_NAME


# C generate define
C_FLAGS = []
C_FLAGS.append('-g')
C_FLAGS.append('-O0')
C_FLAGS.append('-Wall')
C_FLAGS.append('-mcpu=cortex-a9')
C_FLAGS.append('-marm')
C_FLAGS.append('-mfloat-abi=hard')
C_FLAGS.append('-mfpu=neon-vfpv3')
C_FLAGS.append('-mthumb-interwork')

# ASM generate define
AS_FLAGS = []
AS_FLAGS.append(C_FLAGS)

# Link Config
LINK_FLAGS = []
LINK_FLAGS.append('-T')
LINK_FLAGS.append('./link_script.ld')
LINK_FLAGS.append('-Map')
LINK_FLAGS.append(f"map_{TARGET_NAME}.txt")
LINK_FLAGS.append(f"-L{GCC_ARM_PATH}/arm-none-eabi/lib/arm/v5te/hard")
LINK_FLAGS.append(f"-L{GCC_ARM_PATH}/arm-none-eabi/lib")
LINK_FLAGS.append(f"-L{GCC_ARM_PATH}/lib/gcc/arm-none-eabi/9.2.1/arm/v5te/hard")
LINK_FLAGS.append('-lm')
LINK_FLAGS.append('-lgcc')
LINK_FLAGS.append('-lc')
LINK_FLAGS.append('-lnosys')

# Objcopy config
OBJCPY_FLAGS = '-O binary --strip-all --verbose'

# Objdump config
OBJDUMP_FLAGS = '-xDS'

# include file path
# CPP_PATH = Dir('#/include')
CPP_PATH = []
PRJ_ROOT = os.getcwd()
CPP_PATH.append(f'{PRJ_ROOT}/bsp')
CPP_PATH.append(f'{PRJ_ROOT}/bsp/drv_OS_required')
CPP_PATH.append(f'{PRJ_ROOT}/libcpu')
CPP_PATH.append(f'{PRJ_ROOT}/os_include')
CPP_PATH.append(f'{PRJ_ROOT}/components/finsh')
CPP_PATH.append(f'{PRJ_ROOT}/components/driver_frame')


# lib path.
LIB_PATH = []
# .lib, .a file
LIBS_FILES = []

###################################################################################
env = Environment()

env['CC'] = CC
env['AS'] = AS
env['AR'] = AR
env['CXX'] = CXX
env['LINK'] = LINK

env['OBJSUFFIX'] = '.o'
env['LIBPREFIX'] = 'lib'
env['LIBSUFFIX'] = '.a'
env['PROGSUFFIX'] = '.elf'

env.Append(CFLAGS=C_FLAGS)
env.Append(ASFLAGS=AS_FLAGS)
env.Append(LINKFLAGS=LINK_FLAGS)
env.Append(CPPPATH=CPP_PATH)

env['LINKCOM'] = '$LINK -n -o $TARGET $SOURCES $LINKFLAGS'
env['CCCOM'] = '$CC $_CPPINCFLAGS -c $SOURCES -o $TARGET $CFLAGS'
env['ASCOM'] = '$CC $_CPPINCFLAGS -c $SOURCES -o $TARGET $ASFLAGS'

# print(f"----ENV CPPPATH {env['CPPPATH']}")
###################################################################################

src_obj = Glob('*.o')  # src_obj is a list  eg:Program(xx, source=src_obj)

sub_dirs = os.listdir('./')
for d in sub_dirs:
    if os.path.isfile(f'{d}/SConscript'):
        src_obj += SConscript(f'{d}/SConscript', exports='env')



###################################################################################
# Compile target <.elf>, use <$LINKCOM>.
exefile = env.Program(target =TARGET_NO_SUFFIX, source=src_obj)

# # objcopy
env.Command(target=(TARGET_NO_SUFFIX+'.bin'), source=exefile, \
            action=f"{OBJCPY} {OBJCPY_FLAGS} {'$SOURCE'} {'$TARGET'}")

#objdump
env.Command(target=(TARGET_NO_SUFFIX+'.lst'), source=exefile, \
            action=f"{OBJDUMP} {OBJDUMP_FLAGS} {'$SOURCE'} > dump_{TARGET_NAME}.txt")

#size
env.Command(target=(TARGET_NO_SUFFIX+'.size'), source=exefile, \
            action= f"{SIZE} --format=berkeley {'$SOURCE'}")

